package plus;
import java.io.*;

import plus.run.Loader;
import plus.run.VM;

public class Main {
	public static void main(String[] args) throws IOException {
		try {
			if (args.length == 1) {
				build(args[0]);
				VM.exec();
			}
			else if (args.length == 3 && args[0].equals("-o")) {
				build(args[2]);
				VM.bsave(args[1]);
			}
			else if (args.length == 2 && args[0].equals("-r")) {
				VM.reset();
				Loader.builtin.install();
				VM.bload(args[1]);
				VM.exec();
			}
			else usage();
		} catch (Gripe e) {
			e.display();
		}
	}

	private static void build(String main) {
		VM.reset();
		Loader.init();
		Loader.loadFile(new File(main)).compile();
	}

	private static void usage() {
		System.err.println("Usage:");
		System.err.println("\tjava -jar plus.jar filename.plus");
		System.err.println("\tjava -jar plus.jar -o compiled.obj filename.plus");
		System.err.println("\tjava -jar plus.jar -r compiled.obj");
		System.exit(255);
	}

}
